package com.ruoyi.baseMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.baseMain.domain.bo.BasePolicyHisBo;
import com.ruoyi.baseMain.domain.vo.BasePolicyHisVo;
import com.ruoyi.baseMain.domain.BasePolicyHis;
import com.ruoyi.baseMain.mapper.BasePolicyHisMapper;
import com.ruoyi.baseMain.service.IBasePolicyHisService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户政策历史Service业务层处理
 *
 * @author linziheng
 * @date 2024-01-08
 */
@RequiredArgsConstructor
@Service
public class BasePolicyHisServiceImpl implements IBasePolicyHisService {

    private final BasePolicyHisMapper baseMapper;

    /**
     * 查询客户政策历史
     */
    @Override
    public BasePolicyHisVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户政策历史列表
     */
    @Override
    public TableDataInfo<BasePolicyHisVo> queryPageList(BasePolicyHisBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BasePolicyHis> lqw = buildQueryWrapper(bo);
        Page<BasePolicyHisVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户政策历史列表
     */
    @Override
    public List<BasePolicyHisVo> queryList(BasePolicyHisBo bo) {
        LambdaQueryWrapper<BasePolicyHis> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BasePolicyHis> buildQueryWrapper(BasePolicyHisBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BasePolicyHis> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), BasePolicyHis::getNickName, bo.getNickName());
        lqw.like(StringUtils.isNotBlank(bo.getInstanceCode()), BasePolicyHis::getInstanceCode, bo.getInstanceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSignatory()), BasePolicyHis::getSignatory, bo.getSignatory());
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyType()), BasePolicyHis::getPolicyType, bo.getPolicyType());
        lqw.eq(bo.getPolicyId() != null, BasePolicyHis::getPolicyId, bo.getPolicyId());
        lqw.eq(bo.getAccountPoint() != null, BasePolicyHis::getAccountPoint, bo.getAccountPoint());
        lqw.eq(bo.getPrivatePoint() != null, BasePolicyHis::getPrivatePoint, bo.getPrivatePoint());
        lqw.eq(bo.getTotalPoint() != null, BasePolicyHis::getTotalPoint, bo.getTotalPoint());
        lqw.eq(StringUtils.isNotBlank(bo.getOperateUser()), BasePolicyHis::getOperateUser, bo.getOperateUser());
        lqw.like(StringUtils.isNotBlank(bo.getOperateUserName()), BasePolicyHis::getOperateUserName, bo.getOperateUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getApproveUser()), BasePolicyHis::getApproveUser, bo.getApproveUser());
        lqw.like(StringUtils.isNotBlank(bo.getApproveUserName()), BasePolicyHis::getApproveUserName, bo.getApproveUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BasePolicyHis::getStatus, bo.getStatus());
        lqw.orderByDesc(BasePolicyHis::getCreateTime);
        return lqw;
    }

    /**
     * 新增客户政策历史
     */
    @Override
    public Boolean insertByBo(BasePolicyHisBo bo) {
        BasePolicyHis add = BeanUtil.toBean(bo, BasePolicyHis.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户政策历史
     */
    @Override
    public Boolean updateByBo(BasePolicyHisBo bo) {
        BasePolicyHis update = BeanUtil.toBean(bo, BasePolicyHis.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BasePolicyHis entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户政策历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
